package com.paradise.phone.fragment;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.paradise.api.FaceLogin;
import com.paradise.api.FacePhone;
import com.paradise.api.FacephonePreferences;
import com.paradise.phone.R;
import com.paradise.phone.activity.LoginActivity;
import com.paradise.phone.activity.MainActivity;

import org.linphone.core.LinphoneAuthInfo;

/**
 * Created by Sisyphus on 2017/9/9.
 */

public class UserInfoFragment extends Fragment implements View.OnClickListener {

    private View rootView;

    private TextView tvDisplayName;
    private TextView tvUsername;
    private TextView tvDomain;
    private TextView tvProxy;
    private TextView tvTransport;
    private TextView tvVideoSize;
    private Button btnLogout;
    private Button btnLoginRefresh;
    private ImageView ivStatus;
    private ImageView ivNavigation;
    private TextView tvTitle;
    private ImageView ivBackNumpad;

    private LinphoneAuthInfo authInfo;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

        rootView = inflater.from(getActivity()).inflate(com.paradise.phone.R.layout.fragment_user_info, null);

        initView();

        initEvent();

        return rootView;
    }

    private void initView(){
        tvUsername = (TextView) rootView.findViewById(com.paradise.phone.R.id.tv_info_username);
        tvDomain = (TextView) rootView.findViewById(com.paradise.phone.R.id.tv_info_domain);
        tvProxy = (TextView) rootView.findViewById(com.paradise.phone.R.id.tv_info_proxy);
        tvDisplayName = (TextView) rootView.findViewById(com.paradise.phone.R.id.tv_display_name);
        tvTransport = (TextView) rootView.findViewById(com.paradise.phone.R.id.tv_info_transport);
        tvVideoSize = (TextView) rootView.findViewById(com.paradise.phone.R.id.tv_info_size);
        btnLoginRefresh = (Button) rootView.findViewById(com.paradise.phone.R.id.btn_login_refresh);
        btnLogout = (Button) rootView.findViewById(com.paradise.phone.R.id.btn_logout);
        ivStatus = (ImageView) rootView.findViewById(com.paradise.phone.R.id.iv_status);
        ivNavigation = (ImageView) rootView.findViewById(com.paradise.phone.R.id.iv_toolbar_navigation);
        ivBackNumpad = (ImageView) rootView.findViewById(com.paradise.phone.R.id.iv_back_to_numpad);
        tvTitle = (TextView) rootView.findViewById(com.paradise.phone.R.id.tv_toolbar_title);

        if ((authInfo = FaceLogin.instance().getCurrentAuthInfo()) != null){
            tvUsername.setText(authInfo.getUsername());
            tvDomain.setText(authInfo.getDomain());
            int accountCount = FacephonePreferences.instance().getAccountCount();

            tvProxy.setText(getActivity().getString(R.string.proxy));
            String transport = FacephonePreferences.instance().getAccountTransport(accountCount - 1).toString();
            tvTransport.setText(transport.substring(transport.length() - 3));
            tvVideoSize.setText(FacephonePreferences.instance().getPreferredVideoSize());
            tvDisplayName.setText(getActivity().getSharedPreferences("face_phone", Context.MODE_PRIVATE)
                    .getString("display_name", ""));
        }

        if (((MainActivity) getActivity()).getStatus().equals("在线")){
            ivStatus.setVisibility(View.VISIBLE);
        }else {
            ivStatus.setVisibility(View.INVISIBLE);
        }
    }

    private void initEvent(){
        btnLogout.setOnClickListener(this);
        btnLoginRefresh.setOnClickListener(this);
        ivNavigation.setOnClickListener(this);
        ivBackNumpad.setOnClickListener(this);
        tvTitle.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case com.paradise.phone.R.id.btn_login_refresh:
                if (authInfo != null){
                    FaceLogin.instance().regist(getActivity(), getActivity().getSharedPreferences("face_phone", Context.MODE_PRIVATE)
                                    .getString("display_name", ""),
                                    authInfo.getUsername(),
                                    getString(com.paradise.phone.R.string.password),
                                    getString(com.paradise.phone.R.string.domain),
                                    getString(com.paradise.phone.R.string.proxy),
                                    getString(com.paradise.phone.R.string.transport));
                }
                break;

            case com.paradise.phone.R.id.btn_logout:
                FacePhone.instance().getmLc().clearAuthInfos();
                FacePhone.instance().getmLc().clearProxyConfigs();
                startActivity(new Intent(getActivity(), LoginActivity.class));
                getActivity().finish();
                break;

            case com.paradise.phone.R.id.iv_toolbar_navigation:
            case com.paradise.phone.R.id.tv_toolbar_title:
                ((MainActivity)getActivity()).getNavigation().openDrawer(Gravity.LEFT);
                break;

            case com.paradise.phone.R.id.iv_back_to_numpad:
                getFragmentManager().beginTransaction()
                        .replace(com.paradise.phone.R.id.fl_fragment_container, new CallFragment())
                        .commit();
                break;
        }
    }
}
